﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFM.API;
using CFM.DTO;
using CFM.Common;
using System.Text.RegularExpressions;
namespace CFM
{
    public partial class frmTable : Form
    {
        private TableAPI _api = null;

        private TableGroupAPI _tgapi = null;

        public frmTable()
        {
            InitializeComponent();
            _api= new TableAPI();
            _tgapi = new TableGroupAPI();
        }

        private void frmTable_Load(object sender, EventArgs e)
        {
            EnableControl(false);

            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnSave.Enabled = false;
            BindDataGridView();
            Nhomban();
        }

        private void BindDataGridView()
        {
            grvtable.DataSource = _api.GetAll();
        }

        void Nhomban()
        {
            cbbTableGroup.DataSource = _tgapi.GetAll();
            cbbTableGroup.DisplayMember = "TableGroupName";
            cbbTableGroup.ValueMember = "TableGroupId";
        }

        private void EnableControl(bool flag)
        {
            txtTableID.Enabled = flag;
            txtTableName.Enabled = flag;
            cbbTableGroup.Enabled = flag;
        }

        private void grvtable_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            Table table = grvtable.Rows[e.RowIndex].DataBoundItem as Table;
            txtTableID.Text = table.TableId;
            txtTableName.Text = table.TableName;
            cbbTableGroup.SelectedValue = table.TableGroupId;
            EnableControl(false);
            MethodHelper.ClearErrorProvider();

            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            btnSave.Enabled = false;
        }

        private bool CheckBeforeSave()
        {
            bool flag = true;
            if (txtTableID.Enabled && txtTableID.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtTableID, Const.EMPTY_MESSAGE);
                flag = false;
            }
            if (txtTableName.Enabled && txtTableName.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtTableName, Const.EMPTY_MESSAGE);

                flag = false;
            }
            if (!txtTableName.Enabled && !txtTableID.Enabled)
                flag = false;
            return flag;
        }

        private void ResetControl()
        {
            txtTableName.Text = string.Empty;
            txtTableID.Text = string.Empty;
            btnSave.Enabled = true;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            txtTableName.Focus();
            MethodHelper.ClearErrorProvider();
        }        
       
        private void btnInsert_Click(object sender, EventArgs e)
        {
            EnableControl(true);
            ResetControl();

            btnSave.Enabled = true;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;  
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn xóa", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (txtTableID.Text.Length > 0)
                {
                    _api.Delete(txtTableID.Text.Trim());
                    if (_api.Delete(txtTableID.Text.Trim()) == false)
                    {
                        MessageBox.Show("Không thể xóa!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    BindDataGridView();
                    ResetControl();

                    btnDelete.Enabled = false;
                    btnEdit.Enabled = false;
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtTableID.Text.Length > 0)
            {
                EnableControl(true);
                txtTableID.Enabled = false;
                MethodHelper.ClearErrorProvider();

                btnSave.Enabled = true;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckBeforeSave())
                return;
            Table table = new Table();
            table.TableId = txtTableID.Text;
            table.TableName = txtTableName.Text;
            table.TableGroupId = cbbTableGroup.SelectedValue.ToString();
            if (txtTableID.Enabled)
                _api.Create(table);
            else
                _api.Update(table);
            BindDataGridView();
            ResetControl();
            MethodHelper.ClearErrorProvider();

            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
        }

        private void txtTableName_TextChanged(object sender, EventArgs e)
        {
            if (txtTableID.Enabled == true)
            {
                txtTableID.Text = MethodHelper.GetIdFromName(txtTableName.Text) + cbbTableGroup.SelectedValue.ToString();
            }
        }

    }

}
